﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.ServiceModel.Web;
using System.Text;
using System.Threading.Tasks;

namespace OpenApi.OAuths
{
    public class OAuthProvider : IAuthProvider
    {

        /// <summary>
        /// 授权接口
        /// </summary>
        /// <param name="context">上下文信息</param>
        /// <param name="openness">资源的开放度</param>
        /// <returns>授权结果</returns>
        public AuthResult Authenticate(WebOperationContext context, ResourceOpenness openness)
        {
            AuthResult result = new AuthResult();
            try
            {
                if (openness == ResourceOpenness.Public)
                {
                    OauthComponent.Instance.PublicResourceAuthorization(context.IncomingRequest);
                }
                else if (openness == ResourceOpenness.Private)
                {
                    result.UserId = OauthComponent.Instance.PrivateResourceAuthorization(context.IncomingRequest);
                }
                result.Success = true;
            }
            catch (ArgumentException ex)
            {
                result.Success = false;
				MyLogger.Fatal("OperationInvoker", ex);
            }
            return result;
        }
    }
}
